package com.kaigejava.saimacode.t2017;

import java.io.File;

/**
 * filename extension（去哪儿2017校招真题）
 * Please create a  function to extract the filename extension from the given path,return the extracted filename extension or null  if none.
 *
 * 判断给定的文件名称中是否包含了扩展名。如果包含了，获取到扩展名。否则输出null
 */
public class FileNameExtension {

    public static void main(String[] args) {
        String fileName = "Abc/fil.txt";
        String extFileName = getExtFileName(fileName);
        System.out.println(extFileName);
    }

    private static String getExtFileName(String fileName) {
        if(null == fileName || " ".equals(fileName)){
            return null;
        }
        String [] nameArr = fileName.split("/");
        //获取最后一个
        String lastName = nameArr[nameArr.length-1];
        if(lastName.contains(".")){
            String caselsh = lastName.substring(lastName.lastIndexOf(".")+1);
            if(null == caselsh || " ".equals(caselsh) || "".equals(caselsh)){
                return "null";
            }
            return caselsh;
        }


        return null;
    }
}
